//
//  SignUpViewController.m
//  QinRen
//
//  Created by Donny on 15/2/27.
//  Copyright (c) 2015年 Donny. All rights reserved.
//

#import "SignUpViewController.h"
#import "SignupCell.h"
#import "SignupButtonCell.h"
//#import "CustomDatePickerView.h"

@interface SignUpViewController ()<UITextFieldDelegate>
{
    /** 用户名 */
    UITextField * _employeeUserNameField;
    /** 密码 */
    UITextField * _employeeUserPwdField;
    /** 再次输入密码 */
    UITextField * _confirmField;
    /** 手机 */
    UITextField * _employeePhoneField;
    /** 身份证 */
    UITextField * _employeeCredentialscardField;
    /** 店铺地址 */
    UITextField * _deptNameField;
    /** 上级店铺 */
    UITextField * _deptPField;
    /** 省 */
    UITextField * _deptProvinceField;
    /** 市 */
    UITextField * _deptCityField;
    /** 区 */
    UITextField * _deptRegionField;
    /** 地址 */
    UITextField * _deptAddressField;
}
@property (strong, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation SignUpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 20)];
    self.tableView.tableHeaderView.backgroundColor = [UIColor clearColor];
    self.tableView.tableFooterView = [[UIView alloc] init];

   
    
}




- (void)SignUp
{
//    AFNetworkReachabilityStatus status = [[AFNetworkReachabilityManager sharedManager] networkReachabilityStatus];
//    if (status < 1)
//    {
//        [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
//        [SVProgressHUD showErrorWithStatus:@"网络似乎断开了"];
//        return;
//    }
    
    if (_employeeUserNameField.text.length <= 0 || _employeeUserPwdField.text.length <= 0 || _employeePhoneField.text.length <= 0 || _employeeCredentialscardField.text <= 0 || _deptNameField.text.length <= 0 || _deptPField.text.length <= 0 || _deptProvinceField.text.length <= 0 || _deptCityField.text <= 0 || _deptRegionField.text.length <= 0 || _deptAddressField.text.length <= 0)
    {
        [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
        [SVProgressHUD showErrorWithStatus:@"请将信息填写完整"];
        return;
    }
    
    if ([_employeeUserPwdField.text isEqualToString:_confirmField.text] == NO) {
        [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
        [SVProgressHUD showErrorWithStatus:@"两次填写的密码不一致"];
        return;
    }
    
    
    
    EAHttpAPIClient *manager = [EAHttpAPIClient manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *parameters = @{@"employee_userpwd":_employeeUserPwdField.text,@"employee_username":_employeeUserNameField.text,@"employee_phone":_employeePhoneField.text,@"employee_credentialscard":_employeeCredentialscardField.text,@"dept_name":_deptNameField.text,@"dept_p":_deptPField.text,@"dept_province":_deptProvinceField.text,@"dept_city":_deptCityField.text,@"dept_region":_deptRegionField.text,@"dept_address":_deptAddressField.text};
    
    [manager GetWithParameters:parameters method:@"set.manager_info" success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *resposeString ;
        if ([responseObject isKindOfClass:[NSData class]]) {
            resposeString = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
        }else
            resposeString = responseObject;
        
        NSString *jsonString = [DataParser parseString:resposeString];
        id dictionary = [JSONKit jsonToArrayOrDictionary:jsonString];
//        NSLog(@"dictonary :%@",[dictionary description]);
        
        
        if ([[dictionary objectForKey:@"verification"]intValue] >= 1)
        {
            if ([[dictionary objectForKey:@"total"]intValue] <= 0)
            {
                [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
                [SVProgressHUD showErrorWithStatus:[dictionary objectForKey:@"error"]];
            }else
            {
                [self.navigationController popViewControllerAnimated:YES];
            }
            
        }else
        {
            NSString *error = [dictionary objectForKey:@"error"];
            [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
            [SVProgressHUD showErrorWithStatus:error];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
        [SVProgressHUD showErrorWithStatus:[error localizedDescription]];
    }];
}

#pragma mark -
#pragma mark - UITableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 7;
    }
    else
    {
        return 2;
    }
    
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        SignupCell * cell = [tableView dequeueReusableCellWithIdentifier:@"SignupCell"];
        switch (indexPath.row) {
            case 0:
                _employeeUserNameField = cell.textField;
                cell.textField.placeholder = @"姓名";
                break;
            case 1:
                _employeeUserPwdField = cell.textField;
                cell.textField.placeholder = @"密码";
                cell.textField.secureTextEntry=YES;
                break;
            case 2:
                _confirmField= cell.textField;
                cell.textField.placeholder = @"确认密码";
                cell.textField.secureTextEntry=YES;
                break;
            case 3:
                _employeeCredentialscardField = cell.textField;
                cell.textField.placeholder = @"身份证号";
                cell.textField.secureTextEntry=YES;
                break;
            case 4:
                _employeePhoneField = cell.textField;
                cell.textField.placeholder = @"手机";
                break;
            case 5:
                _deptAddressField = cell.textField;
                cell.textField.placeholder = @"店铺编号";
                break;
            case 6:
                _deptPField = cell.textField;
                cell.textField.placeholder = @"上级店铺";
                break;
            default:
                break;
        }
        return cell;
    }
    else if (indexPath.section == 1)
    {
        if(indexPath.row == 0)
        {
            UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"cell"];
            if (!cell)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
                cell.backgroundColor = [UIColor clearColor];
                return cell;
                
            }
        }
        else
        {
            SignupButtonCell * cell = [tableView dequeueReusableCellWithIdentifier:@"SignupButtonCell"];
            [cell.signupButton addTarget:self action:@selector(SignUp) forControlEvents:UIControlEventTouchUpInside];
            return cell;
        }
        
        
    }
    return nil;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return 45;
    }
    else
    {
        if (indexPath.row == 0)
        {
            return 25;
        }
        return 50;
    }
}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    if (section == 1)
//    {
//        return 0;
//    }
//    return 0;
//}
//不让组头跟随
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.tableView)
    {
        CGFloat sectionHeaderHeight = 25;
        if (scrollView.contentOffset.y <= sectionHeaderHeight&&scrollView.contentOffset.y >= 0)
        {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        }
        else if (scrollView.contentOffset.y >= sectionHeaderHeight)
        {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}



- (void)textFieldDidBeginEditing:(UITextField *)textField
{
  
        //
//    if(textField==birthField)
//    {
//     
//        
//        if(!self.tabBarController.tabBar.hidden)
//        {
//            self.tabBarController.tabBar.hidden=YES;
////            CustomDatePickerView *pickView=[[CustomDatePickerView alloc] init];
////            [pickView setCommitBlock:^(NSString *result) {
////                
////                birthField.text=result;
////                
////            }];
////           [self.view addSubview:pickView];
//            [self.view endEditing:YES];
//
//        }
//        
//    }
//   if(textField==sexField)
//    {
//     
//        if(!self.tabBarController.tabBar.hidden)
//        {
//            self.tabBarController.tabBar.hidden=YES;
//        
//        myActionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:NSLocalizedString(@"取消", nil) destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(@"男", nil),NSLocalizedString(@"女",nil), nil];
//        myActionSheet.tag = 1001;
//        [myActionSheet showInView:self.view];
//
//        //   [self.view addSubview:myActionSheet];
//        }
//        
         [self.view endEditing:YES];
        
 //   }
}

#pragma mark  UIActionSheet
//- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
//{
//    if (buttonIndex==0) {
//        
//        sexField.text=@"男";
//    }
//    else
//    {
//        sexField.text=@"女";
//    }
//}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    
    return YES;
    
}

@end
